package com.wwy.arithmetic.newsuanfa;

/**
 * @author 王伟羽
 * @program: arithmetictest
 * @description:
 * @create: 2025/03/31
 */

public class zhengshufanzhuan {
    public int reverse(int x) {
        boolean flag = x < 0;
        String s = x+"";
        s = flag ?s.substring(1,s.length()):s;
        char[] chars = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {
            sb.insert(0,aChar);
        }
        Long aLong = Long.valueOf(sb.toString());
        if(aLong>2147483648L){
            return 0;
        }else{
            int i = Integer.parseInt(sb.toString());
            return flag? i-2*i:i;
        }
    }

    public int reverse1(int x) {
        boolean flag = x>0;
        String target  = x+"";
        target = flag?target:target.substring(0,target.length()-1);
        char[] chars = target.toCharArray();
        StringBuilder msg = new StringBuilder();
        for (char aChar : chars) {
            msg.insert(0,aChar);
        }
        String s = msg.toString();
        Long num = Long.valueOf(s);
        if(num>2147483648L){
            return 0;
        }
        else{
            Integer integer = Integer.valueOf(num.toString());
            return flag?integer:0-integer;
        }

    }


}
